#include "GTTcpServer.h"
#include <memory.h>
#include <pthread.h>
#include "GTTcpConnectionForImageReceive.h"

#define _BACKLOG_ 10


GTTcpServer::GTTcpServer(const string& strIp, int nPort)
	:m_strServerIp(strIp),
	m_nPort(nPort),
    m_pConn(nullptr)
{
	std::cout<<"Main thread Thread_ID = "<< pthread_self()<<std::endl;

       	m_nSocket = socket(AF_INET,SOCK_STREAM,0);
    	if(m_nSocket < 0)
    	{
		cout<<"create socket error."<<endl;
    	}
    	struct sockaddr_in server_socket;
    	struct sockaddr_in client_socket;
    	
//	bzero(&server_socket,sizeof(server_socket));

    	server_socket.sin_family = AF_INET;
    	server_socket.sin_port = htons(m_nPort);
    	server_socket.sin_addr.s_addr =inet_addr(m_strServerIp.c_str())/*htonl(INADDR_ANY)*/;

    	if(bind(m_nSocket,(struct sockaddr *)&server_socket,sizeof(struct sockaddr_in)) < 0)
    	{
//		printf("bind error,error code is %d,error string is :%s\n",errno,strerror(errno));
		close(m_nSocket);
//		return 1;
    	}
    	if(listen(m_nSocket,_BACKLOG_) < 0)
    	{
//		printf("listen error,error code is %d,error string is %s\n",errno,strerror(errno));
		close(m_nSocket);
//		return 2;
    	}
    	printf("bind and listen success,wait accept...\n");

	pthread_t tid;
	pthread_create(&tid, nullptr, &GTTcpServer::ListenThreadFunc, this);

#if 0

	for(;;)
    {
        socklen_t len = 0;
        int client_sock = accept(sock,(struct sockaddr *)&client_socket,&len);
        if(client_sock < 0)
        {
  //          printf("accept error, error is %d,errstring is %s\n",errno,strerror(errno));
            close(sock);
//            return 3;
        }
//        char buf_ip[INET_ADDRSTRLEN];
       // memset(buf_ip,'\0',sizeof(buf_ip));
       // inet_ntop(AF_INET,&client_socket.sin_addr,buf_ip,sizeof(buf_ip));



	std::cout<<inet_ntoa(client_socket.sin_addr)<<std::endl;
	std::cout<<client_socket.sin_port<<std::endl;

//      printf("%s", client_socket.sin_addr);

       // printf("get connect,ip is %s,port is %d\n",buf_ip,ntohs(client_socket.sin_port));
        while(1)
        {
            char buf[1024];
            memset(buf,'\0',sizeof(buf));//跟前面的初始化对比
            read(client_sock,buf,sizeof(buf));
            printf("client :# %s\n",buf);
            printf("server :$");
            memset(buf,'\0',sizeof(buf));
            fgets(buf,sizeof(buf),stdin);
            buf[strlen(buf)-1] = '\0';
            write(client_sock,buf,strlen(buf)+1);//在这里strlen(buf)+1和sizeof(buf)的区别
            printf("please wait...\n");

        }
    }

#endif
}

GTTcpServer::~GTTcpServer()
{


}

void* GTTcpServer::ListenThreadFunc(void* arg)
{
	std::cout<<"Thread_ID = "<<pthread_self()<<std::endl;
	std::cout<<"listening"<<std::endl;
	GTTcpServer* pTcpServer = static_cast<GTTcpServer*>(arg);
	 for(;;)
    {
        socklen_t len = 0;
	struct sockaddr_in client_socket;

	std::cout<<"server socket: "<< pTcpServer->m_nSocket<< std::endl;
        int client_sock = accept(pTcpServer->m_nSocket,(struct sockaddr *)&client_socket,&len);
        if(client_sock < 0)
	{

		std::cout<<"eoror"<<std::endl;
  //          printf("accept error, error is %d,errstring is %s\n",errno,strerror(errno));
            close(pTcpServer->m_nSocket);
//            return 3;
        }
//        char buf_ip[INET_ADDRSTRLEN];
       // memset(buf_ip,'\0',sizeof(buf_ip));
       // inet_ntop(AF_INET,&client_socket.sin_addr,buf_ip,sizeof(buf_ip));



        std::cout<<inet_ntoa(client_socket.sin_addr)<<std::endl;
        std::cout<<client_socket.sin_port<<std::endl;

//      printf("%s", client_socket.sin_addr);

       // printf("get connect,ip is %s,port is %d\n",buf_ip,ntohs(client_socket.sin_port));
       //

	pTcpServer->NewConnectionHandler(client_socket, client_sock);
#if 0       
       	while(1)
        {
            char buf[1024];
            memset(buf,'\0',sizeof(buf));//跟前面的初始化对比
            read(client_sock,buf,sizeof(buf));
            printf("client :# %s\n",buf);
            printf("server :$");
            memset(buf,'\0',sizeof(buf));
            fgets(buf,sizeof(buf),stdin);
            buf[strlen(buf)-1] = '\0';
            write(client_sock,buf,strlen(buf)+1);//在这里strlen(buf)+1和sizeof(buf)的区别
            printf("please wait...\n");

        }

#endif
    }

	return nullptr;
}	

void  GTTcpServer::NewConnectionHandler(const sockaddr_in& stClientSocket, int nNewSock)
{
	std::cout<<"NewConnectionHandler Thread_ID = "<<pthread_self()<<std::endl;

    m_pConn = new GTTcpConnectionForImageReceive(stClientSocket, nNewSock);   
}
